package daily;

import java.util.HashSet;
import java.util.Set;

/**
 * @author MtRider
 * @version 1.0
 * @description 存在重复元素
 * @GiteeAndGithub MtRider
 * @date 2022/4/21 18:23
 */
public class demo04 {
    public static void main(String[] args) {
        int[] nums = {1, 1, 1, 3, 3, 4, 3, 2, 4, 2};
        //System.out.println(containsDuplicate(nums));
        System.out.println(containsDuplicate2(nums));
    }

    //暴力破解 查重
    public static boolean containsDuplicate(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] == nums[j]) {
                    return true;
                }
            }
        }
        return false;
    }
    //Hashset去重
    public static boolean containsDuplicate2(int []nums){
        Set<Integer> hashSet = new HashSet<>();
        for (int num : nums) {
            if(!hashSet.add(num)){
                return true;
            }
        }
        return false;
    }
}
